/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.Utilities
*        文件名:             TableConfigHelper.cs
*        创建系统时间:       2012/11/2 19:07:08
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.Utilities
{
    using System;
    using System.Reflection;
    using System.Xml;

    public class TableConfigHelper
    {
        private string fileName = string.Empty;
        public string SelectPath = "//resultMaps/resultMap/result";

        public void GetConfig(string fileName, object table)
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(fileName);
            Type type = table.GetType();
            string key = string.Empty;
            string str2 = string.Empty;
            foreach (FieldInfo info in type.GetFields(BindingFlags.Public | BindingFlags.Static))
            {
                if (info.Name.StartsWith("Field"))
                {
                    key = info.Name.Substring("Field".Length);
                    str2 = this.GetValue(xmlDocument, key);
                    info.SetValue(table, str2);
                }
            }
        }

        public string GetValue(string key)
        {
            return this.GetValue(this.FielName, this.SelectPath, key);
        }

        public string GetValue(string fileName, string key)
        {
            return this.GetValue(fileName, this.SelectPath, key);
        }

        public string GetValue(XmlDocument xmlDocument, string key)
        {
            return this.GetValue(xmlDocument, this.SelectPath, key);
        }

        public string GetValue(string fileName, string selectPath, string key)
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(fileName);
            return this.GetValue(xmlDocument, selectPath, key);
        }

        public string GetValue(XmlDocument xmlDocument, string selectPath, string key)
        {
            string str = string.Empty;
            foreach (XmlNode node in xmlDocument.SelectNodes(selectPath))
            {
                if (node.Attributes["property"].Value.ToUpper().Equals(key.ToUpper()))
                {
                    return node.Attributes["column"].Value;
                }
            }
            return str;
        }

        public string FielName
        {
            get
            {
                return this.fileName;
            }
            set
            {
                this.fileName = value;
            }
        }
    }
}